//
//  BKDialPlateView.m
//  XPhoto
//
//  Created by baboy on 2019/2/22.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "BKDialPlateView.h"
#import "NSAttributedString+x.h"
#define DIAL_PLATE_VIEW_TEXT_FONT [UIFont systemFontOfSize:14]
#define DIAL_PLATE_VIEW_HOR_PADDING 10
#define DIAL_PLATE_VIEW_VER_PADDING 3
#define DIAL_PLATE_VIEW_HEIGHT      24

@interface BKDialPlateView()<UIScrollViewDelegate>
@property(nonatomic, strong) NSMutableArray* itemViews;
@property(nonatomic, assign) CGPoint dragStartOffset;
@property(nonatomic, strong) UIView* anchorView;
@end

@implementation BKDialPlateView
- (void)awakeFromNib{
    [super awakeFromNib];
    if (!_textColor) {
        _textColor = [UIColor whiteColor];
    }
    self.selectedIndex = -1;
    [self setup];
}
- (void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    self.selectedIndex = _selectedIndex;
    DLOG(@"BKDialPlateView frame:%@,%@", NSStringFromCGRect(self.bounds), NSStringFromCGPoint(self.scrollView.contentOffset));
}
- (void)setTextColor:(UIColor *)textColor{
    _textColor = textColor;
    if (self.anchorView) {
        self.anchorView.backgroundColor = textColor;
    }
    for (UIButton *b in self.itemViews) {
        [b setTitleColor:textColor forState:UIControlStateNormal];
    }
}
- (void)setup{
    self.selectedIndex = -1;
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        [self addSubview:_scrollView];
        _scrollView.delegate = self;
    }
    if (!_anchorView) {
        _anchorView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 0, 2)];
        _anchorView.backgroundColor = self.textColor;
        [self.scrollView addSubview:_anchorView];
    }
}
- (void)setItems:(NSArray *)items{
    if(self.itemViews){
        [self.itemViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
    NSMutableArray *itemViews = [NSMutableArray array];
    for (int i = 0; i<[items count]; i++) {
        NSDictionary *item = [items objectAtIndex:i];
        NSString* title = [item valueForKey:@"title"];
        
        UIButton *itemView = [UIButton buttonWithType:UIButtonTypeCustom];
        [itemView setTag:i];
        [itemView setTitle:title forState:UIControlStateNormal];
        [itemView setTitleColor:self.textColor forState:UIControlStateNormal];
        itemView.titleLabel.font = DIAL_PLATE_VIEW_TEXT_FONT;
        [self.scrollView addSubview:itemView];
        [itemViews addObject:itemView];
        
        [itemView addTarget:self action:@selector(clickItem:) forControlEvents:UIControlEventTouchUpInside];
    }
    self.itemViews = itemViews;
    _items = items;
    [self layoutItemViews];
}
- (void)layoutItemViews{
    if (!self.itemViews) {
        return;
    }
    CGRect itemFrame = CGRectMake(0, 0, (self.scrollView.bounds.size.height - DIAL_PLATE_VIEW_HEIGHT)/2, self.scrollView.bounds.size.height-2);
    CGFloat margin = 20;
    for (int i = 0; i<[self.itemViews count]; i++) {
        UIButton *itemView = [self.itemViews objectAtIndex:i];
        CGSize titleSize = [itemView.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:itemView.titleLabel.font}];
        itemFrame.size.width = titleSize.width + 2*margin;
        itemView.frame = itemFrame;
        
        itemFrame.origin.x += itemFrame.size.width;
    }
    
    CGRect anchorFrame = CGRectMake(0, itemFrame.size.height, 0, 2);
    self.anchorView.frame = anchorFrame;
    self.selectedIndex = _selectedIndex;
}
- (void)clickItem:(id)btn{
    [self setSelectedIndex:[btn tag]];
}
- (void)setSelectedIndex:(NSInteger)selectedIndex{
    if (selectedIndex < 0 || selectedIndex >= self.items.count) {
        return;
    }
    if(selectedIndex>=0){
        self.anchorView.hidden = NO;
    }
    
    UIButton *itemView = [self.itemViews objectAtIndex:selectedIndex];
    CGFloat offsetX = self.bounds.size.width/2 - itemView.bounds.size.width/2 - itemView.frame.origin.x;
    
    CGRect anchorFrame = self.anchorView.frame;
    anchorFrame.size.width = itemView.frame.size.width;
    anchorFrame.origin.x = itemView.frame.origin.x;
    anchorFrame = CGRectInset(anchorFrame, 20, 0);
    
    [UIView animateWithDuration:0.1 animations:^{
        [self.scrollView setContentInset:UIEdgeInsetsMake(0, offsetX, 0, 0) ];
        self.anchorView.frame = anchorFrame;
    }];
    if(self.delegate && selectedIndex != self.selectedIndex){
        id checkedItem = [self.items objectAtIndex:selectedIndex];
        [self.delegate dialPlateView:self didSelectAtIndex:selectedIndex withItem:checkedItem];
    }
    _selectedIndex = selectedIndex;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    self.dragStartOffset = scrollView.contentOffset;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    CGFloat offsetX = scrollView.contentOffset.x - self.dragStartOffset.x;
    
    NSInteger index = self.selectedIndex + (offsetX<0?-1:+1);
    index = RANGE_VAL(0, self.items.count-1, index);
    self.selectedIndex = index;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    [self layoutItemViews];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
